package one.stand.controller.supplier;

import com.jmp.api.dto.OrdersSendRequest;
import com.jmp.api.dto.SupplierOrderRequest;
import com.jmp.api.dto.SupplierOrderResponse;
import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import com.jmp.base.dto.OrderListDto;
import com.jmp.base.dto.OrderListVo;
import com.jmp.base.dto.OrderSendRequest;
import com.jmp.feign.OrderFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import one.stand.dto.ConvertUtils;
import one.stand.dto.PageResponse2;
import one.stand.dto.UserDto;
import one.stand.enums.OrderStateEnum;
import one.stand.enums.RefundStateEnum;
import one.stand.model.ResultModel;
import one.stand.service.user.UserAdminService;
import one.stand.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * @author cjr
 */
@Api(description = "店铺-订单")
@RequestMapping("supplier/order")
@RestController
public class SupplierOrderController {

    @Autowired
    private OrderFeign orderFeign;

    @Autowired
    private UserAdminService userAdminService;

    @ApiOperation("1列表")
    @GetMapping("list")
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel<SupplierOrderResponse> list(@RequestAttribute SupplierOrderRequest request) {
        final RequestContext.User optUser = RequestContextHolder.getRequestContext().getUser();
        ResultModel<OrderListVo> resultModel = orderFeign.list(OrderListDto.builder()
                .companyId(optUser.getCompanyId())
                .status(request.getStatus())
                .keyword(request.getKeyword())
                .build());
        AssertUtil.isTrue(resultModel);

        PageResponse2<SupplierOrderResponse.Order> orderPage = ConvertUtils.page2(resultModel.getData().getOrderPage(), order -> {

            UserDto user = userAdminService.get(optUser.getUserId());
            Integer type = null;
            if (order.getOrderState().equals(OrderStateEnum.WAIT_PAYMENT.getCode())) {
                type = 1;
            } else if (order.getRefundState().equals(RefundStateEnum.REFUND_APPLY.getCode())) {
                type = 6;
            } else if (order.getOrderState().equals(OrderStateEnum.WAIT_DELIVER.getCode())) {
                type = 2;
            } else if (order.getOrderState().equals(OrderStateEnum.WAIT_RECEIVING.getCode())) {
                type = 3;
            } else if (order.getOrderState().equals(OrderStateEnum.COMPLETE.getCode())) {
                type = 4;
            } else if (order.getOrderState().equals(OrderStateEnum.REFUND_SUCCESS.getCode())) {
                type = 7;
            } else if (order.getOrderState().equals(OrderStateEnum.CANCEL.getCode())
                    || order.getOrderState().equals(OrderStateEnum.BE_OVERDUE.getCode())) {
                type = 5;
            }

            return SupplierOrderResponse.Order.builder()
                    .ordersId(order.getOrderId())
                    .orderPrice(MoneyUtil.getMoneyInt(order.getOrderPrice()))
                    .orderStatus(type)

                    .cancelFlag(order.getCancelFlag())
                    .sendFlag(order.getSendFlag())
                    .refundRefuseFlag(order.getRefundCancelFlag())
                    .refundAgreeFlag(order.getRefundCancelFlag())

                    .userId(user.getUserId())
                    .userImg(OneStringUtils.photoResize(user.getUserImg()))
                    .userName(user.getName())

                    .goodsList(ConvertUtils.list(order.getGoodsList(), goods -> {
                        return SupplierOrderResponse.Goods.builder()
                                .goodsId(goods.getGoodsId())
                                .goodsName(goods.getGoodsName())
                                .goodsPhoto(OneStringUtils.photoResize(goods.getGoodsPhoto()))
                                .skuName(goods.getSkuName())
                                .skuNum(goods.getSkuNum())
                                .price(goods.getPrice())
                                .price(goods.getPrice().multiply(BigDecimal.valueOf(goods.getSkuNum())))
                                .build();
                    }))

                    .build();
        });

        SupplierOrderResponse response = SupplierOrderResponse.builder()
                .orderPage(orderPage)
                .build();
        return ResultModel.success(response);
    }

    @ApiOperation(value = "2确认发货")
    @PostMapping("/send")
    @ResponseBody
    @SecurityAuthCheck(company = true, card = true)
    public ResultModel send(@RequestAttribute OrdersSendRequest request) {

        ResultModel resultModel = orderFeign.send(OrderSendRequest.builder()
                .orderId(request.getOrderId())
                .deliveryCompany(request.getDeliveryCompany())
                .deliveryNo(request.getDeliveryNo())
                .build());
        AssertUtil.isTrue(resultModel);

        return ResultModel.success();
    }
}
